package com.service.admin.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.admin.dto.LoginUserSession;
import com.service.admin.product.controller.rep.ProductDetailRep;
import com.service.core.exception.CommonException;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.enums.ProductEnums;
import com.service.dao.mapper.ProductOtherMapper;
import com.service.dao.model.ProductOther;
import com.service.admin.product.controller.rep.ProductPageDetailRep;
import com.service.admin.product.controller.req.ProductPageReq;
import com.service.admin.product.controller.req.ProductAddReq;
import com.service.admin.product.controller.req.UpdateNumReq;
import com.service.admin.product.service.ProductOtherService;
import com.service.admin.support.SessionUtil;
import com.service.utility.modelmapper.ModelMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年10月09日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
public class ProductOtherServiceImpl extends ServiceImpl<ProductOtherMapper, ProductOther> implements ProductOtherService {

    @Autowired
    private SessionUtil sessionUtil;

    @Override
    public CommonPageRep<ProductPageDetailRep> pageProduct(ProductPageReq pageReq) {
        Page<ProductOther> page = PageHelper.startPage(pageReq.getPageNo(), pageReq.getPageSize());
        LambdaQueryWrapper<ProductOther> wrapper = new QueryWrapper<ProductOther>().lambda();
        if (StringUtils.isNotBlank(pageReq.getProductName())) {
            wrapper.like(ProductOther::getProductName, pageReq.getProductName());
        }
        if (!ObjectUtils.isEmpty(pageReq.getProductType())) {
            wrapper.eq(ProductOther::getProductType, pageReq.getProductType());
        }
        if (!ObjectUtils.isEmpty(pageReq.getCategory())) {
            wrapper.eq(ProductOther::getCategoryId, pageReq.getCategory());
        }

        wrapper.eq(ProductOther::getLanguage, pageReq.getLanguage().getCode());
        wrapper.eq(ProductOther::getProductStatus, pageReq.getProductStatus().getCode());
        wrapper.orderByAsc(ProductOther::getSort);
        baseMapper.selectList(wrapper);

        List<ProductPageDetailRep> list = page.getResult().stream()
                .map(item -> ModelMapperUtil.getStrictModelMapper().map(item, ProductPageDetailRep.class)).collect(Collectors.toList());

        return CommonPageRep.buildResultRep(page, list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProduct(ProductAddReq productAddReq) {
        ProductOther productOther = ModelMapperUtil.getStrictModelMapper().map(productAddReq, ProductOther.class);
        LoginUserSession session = sessionUtil.getUser();
        productOther.setLanguage(productAddReq.getProductLanguage().getCode());
        productOther.setBuyNum(0);
        productOther.setFakeBuyNum(0);
        productOther.setCreateTime(new Date());
        productOther.setCreateUserId(session.getId());
        productOther.setCreateUserName(session.getNickName());
        productOther.setCreateLoginName(session.getLoginName());
        productOther.setProductStatus(CommonStatusEnums.ACTIVE.getCode());
        if (ObjectUtils.isEmpty(productOther.getLimitBuyNum())) {
            productOther.setLimitBuyNum(0);
        }
        baseMapper.insert(productOther);

    }

    @Override
    public ProductDetailRep detail(Integer id) {
        ProductOther productOther = getById(id);
        if (ObjectUtils.isEmpty(productOther)) {
            throw new CommonException("该商品不存在");
        }

        return ModelMapperUtil.getStrictModelMapper().map(productOther, ProductDetailRep.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyProduct(ProductAddReq addReq) {
        assertProductExist(addReq.getId());
        ProductOther detail = ModelMapperUtil.getStrictModelMapper().map(addReq, ProductOther.class);
        detail.setLanguage(addReq.getProductLanguage().getCode());

        baseMapper.updateById(detail);
    }

    private void assertProductExist(Integer productId) {
        ProductOther productOther = baseMapper.selectById(productId);
        if (ObjectUtils.isEmpty(productOther)) {
            throw new CommonException("该商品不存在");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProductStatus(Integer id, ProductEnums.Status status) {
        assertProductExist(id);
        ProductOther productOther = new ProductOther();
        productOther.setId(id);
        productOther.setProductStatus(status.getCode());
        baseMapper.updateById(productOther);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStockNum(UpdateNumReq numReq) {
        assertProductExist(numReq.getId());
        ProductOther productOther = new ProductOther();
        productOther.setId(numReq.getId());
        productOther.setStock(numReq.getNumber());
        baseMapper.updateById(productOther);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSort(UpdateNumReq numReq) {
        assertProductExist(numReq.getId());
        ProductOther productOther = new ProductOther();
        productOther.setId(numReq.getId());
        productOther.setSort(numReq.getNumber());
        baseMapper.updateById(productOther);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBuyNum(UpdateNumReq numReq) {
        assertProductExist(numReq.getId());
        ProductOther productOther = new ProductOther();
        productOther.setId(numReq.getId());
        productOther.setFakeBuyNum(numReq.getNumber());
        baseMapper.updateById(productOther);
    }


}
